﻿package core.image
{
    import __AS3__.vec.*;
    import flash.display.*;
    import flash.geom.*;

    public class Selection extends Object
    {
        public var outline:Vector.<OutlinePoint>;
        public var selected:Rectangle;
        public var ox:int = 0;
        public var oy:int = 0;
        private var mask:BitmapData;
        var cur:int;

        public function Selection(param1:int, param2:int)
        {
            this.mask = new BitmapData(param1, param2, false, 0);
            this.outline = new Vector.<OutlinePoint>;
            this.selected = new Rectangle();
            return;
        }// end function

        public function Dispose() : void
        {
            this.mask.dispose();
            this.mask = null;
            this.outline = null;
            this.selected = null;
            return;
        }// end function

        public function Move(Selection.as$236:Point) : void
        {
            this.ox = this.ox + Selection.as$236.x;
            this.oy = this.oy + Selection.as$236.y;
            return;
        }// end function

        public function ResetOffset() : void
        {
            var _loc_1:* = this.mask.clone();
            this.mask.fillRect(this.mask.rect, 0);
            this.mask.copyPixels(_loc_1, _loc_1.rect, new Point(this.ox, this.oy));
            this.ox = 0;
            this.oy = 0;
            this.CreateOutline();
            return;
        }// end function

        public function EditPoint(Selection.as$236:int, Selection.as$236:int, Selection.as$236:int, Selection.as$236:Boolean) : void
        {
            if (Selection.as$236 != 255 && this.outline.length > 0)
            {
                this.cur = this.mask.getPixel(Selection.as$236, Selection.as$236) & 255;
                Selection.as$236 = this.cur + Selection.as$236 ? (-Selection.as$236) : (Selection.as$236);
                if (Selection.as$236 < 0)
                {
                    Selection.as$236 = 0;
                }
                if (Selection.as$236 > 255)
                {
                    Selection.as$236 = 255;
                }
                if (Selection.as$236 != this.cur)
                {
                    this.mask.setPixel(Selection.as$236, Selection.as$236, Selection.as$236 << 16 | Selection.as$236 << 8 | Selection.as$236);
                }
            }
            else
            {
                if (Selection.as$236)
                {
                    Selection.as$236 = 255 - Selection.as$236;
                }
                this.mask.setPixel(Selection.as$236, Selection.as$236, Selection.as$236 << 16 | Selection.as$236 << 8 | Selection.as$236);
            }
            return;
        }// end function

        public function EditRectangle(Selection.as$236:Rectangle, Selection.as$236:int, Selection.as$236:Boolean) : void
        {
            var _loc_4:* = new BitmapData(this.mask.width, this.mask.height, true, 16777215);
            _loc_4.fillRect(Selection.as$236, Selection.as$236 ? (4278190080) : (4294967295));
            if (Selection.as$236 > 0)
            {
                Misc.Feather(_loc_4, Selection.as$236);
            }
            this.mask.draw(_loc_4);
            return;
        }// end function

        public function EditEllipse(Selection.as$236:Rectangle, Selection.as$236:int, Selection.as$236:Boolean, Selection.as$236:Boolean) : void
        {
            var _loc_5:* = new Sprite();
            _loc_5.graphics.beginFill(Selection.as$236 ? (0) : (16777215));
            _loc_5.graphics.drawEllipse(Selection.as$236.x, Selection.as$236.y, Selection.as$236.width, Selection.as$236.height);
            _loc_5.graphics.endFill();
            var _loc_6:* = new BitmapData(this.mask.width, this.mask.height, true, 16777215);
            _loc_6.draw(_loc_5);
            if (!Selection.as$236)
            {
                Misc.DeAntialias(_loc_6);
            }
            if (Selection.as$236 > 0)
            {
                Misc.Feather(_loc_6, Selection.as$236);
            }
            this.mask.draw(_loc_6);
            return;
        }// end function

        public function EditPolygon(Selection.as$236:Vector.<Point>, Selection.as$236:int, Selection.as$236:Boolean, Selection.as$236:Boolean) : void
        {
            var _loc_5:* = Selection.as$236[0];
            var _loc_6:* = new Sprite();
            _loc_6.graphics.beginFill(Selection.as$236 ? (0) : (16777215));
            _loc_6.graphics.moveTo(_loc_5.x, _loc_5.y);
            var _loc_7:* = 1;
            while (_loc_7 < Selection.as$236.length)
            {
                
                _loc_5 = Selection.as$236[_loc_7];
                _loc_6.graphics.lineTo(_loc_5.x, _loc_5.y);
                _loc_7 = _loc_7 + 1;
            }
            _loc_5 = Selection.as$236[0];
            _loc_6.graphics.lineTo(_loc_5.x, _loc_5.y);
            _loc_6.graphics.endFill();
            var _loc_8:* = new BitmapData(this.mask.width, this.mask.height, true, 16777215);
            _loc_8.draw(_loc_6);
            if (!Selection.as$236)
            {
                Misc.DeAntialias(_loc_8);
            }
            if (Selection.as$236 > 0)
            {
                Misc.Feather(_loc_8, Selection.as$236);
            }
            this.mask.draw(_loc_8);
            return;
        }// end function

        public function SelectAll() : void
        {
            this.mask.fillRect(this.mask.rect, 16777215);
            return;
        }// end function

        public function Invert() : void
        {
            var _loc_1:* = new ColorMatrix();
            _loc_1.Invert();
            this.mask.applyFilter(this.mask, this.mask.rect, this.mask.rect.topLeft, _loc_1.filter);
            return;
        }// end function

        public function Reset() : void
        {
            this.mask.fillRect(this.mask.rect, 0);
            this.outline = new Vector.<OutlinePoint>;
            this.selected = new Rectangle();
            return;
        }// end function

        public function IsSelected(x:int, x:int) : Boolean
        {
            if (this.selected.contains(x, x))
            {
                return Boolean(this.mask.getPixel(x, x) > 4210752);
            }
            return false;
        }// end function

        public function Max(mask:int, mask:int) : int
        {
            if (this.selected.contains(mask, mask))
            {
                return this.mask.getPixel(mask, mask) & 255;
            }
            return 0;
        }// end function

        public function AlphaMask(core.image:Selection:Rectangle = null) : BitmapData
        {
            if (core.image:Selection == null)
            {
                core.image:Selection = this.mask.rect;
            }
            var _loc_2:* = new BitmapData(core.image:Selection.width, core.image:Selection.height, true, 16777215);
            _loc_2.copyChannel(this.mask, core.image:Selection, this.mask.rect.topLeft, 4, 8);
            return _loc_2;
        }// end function

        public function InvertedAlphaMask(core.image:Selection:Rectangle = null) : BitmapData
        {
            var _loc_2:* = this.AlphaMask(core.image:Selection);
            var _loc_3:* = new ColorMatrix();
            _loc_3.InvertAlpha();
            _loc_2.applyFilter(_loc_2, _loc_2.rect, _loc_2.rect.topLeft, _loc_3.filter);
            return _loc_2;
        }// end function

        public function get rect() : Rectangle
        {
            return this.selected;
        }// end function

        public function get width() : int
        {
            return this.selected.width;
        }// end function

        public function get height() : int
        {
            return this.selected.height;
        }// end function

        public function CreateOutline() : void
        {
            var _loc_1:Boolean = false;
            var _loc_2:Boolean = false;
            var _loc_3:Boolean = false;
            var _loc_4:Boolean = false;
            var _loc_5:Boolean = false;
            var _loc_6:int = 0;
            this.outline = new Vector.<OutlinePoint>;
            this.selected = this.mask.getColorBoundsRect(16777215, 0, false);
            var _loc_7:* = this.selected.top;
            while (_loc_7 <= this.selected.bottom)
            {
                
                _loc_2 = true;
                _loc_3 = Boolean(this.mask.getPixel(this.selected.left, _loc_7) < 4210752);
                _loc_6 = this.selected.left;
                while (_loc_6 <= this.selected.right)
                {
                    
                    _loc_1 = _loc_2;
                    _loc_2 = _loc_3;
                    _loc_3 = Boolean(this.mask.getPixel((_loc_6 + 1), _loc_7) < 4210752);
                    if (!_loc_2)
                    {
                        _loc_4 = Boolean(this.mask.getPixel(_loc_6, (_loc_7 - 1)) < 4210752);
                        _loc_5 = Boolean(this.mask.getPixel(_loc_6, (_loc_7 + 1)) < 4210752);
                        if (_loc_1 && _loc_4 && _loc_3 && _loc_5)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 8));
                        }
                        else if (_loc_1 && _loc_4 && _loc_5)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 9));
                        }
                        else if (_loc_3 && _loc_4 && _loc_5)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 10));
                        }
                        else if (_loc_3 && _loc_4 && _loc_1)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 11));
                        }
                        else if (_loc_3 && _loc_5 && _loc_1)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 12));
                        }
                        else if (_loc_1 && _loc_3)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 13));
                        }
                        else if (_loc_5 && _loc_4)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 14));
                        }
                        else if (_loc_1 && _loc_4)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 0));
                        }
                        else if (_loc_4 && _loc_3)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 2));
                        }
                        else if (_loc_1 && _loc_5)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 5));
                        }
                        else if (_loc_5 && _loc_3)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 7));
                        }
                        else if (_loc_4)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 1));
                        }
                        else if (_loc_1)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 3));
                        }
                        else if (_loc_3)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 4));
                        }
                        else if (_loc_5)
                        {
                            this.outline.push(new OutlinePoint(_loc_6, _loc_7, 6));
                        }
                    }
                    _loc_6++;
                }
                _loc_7++;
            }
            return;
        }// end function

        public function Clone() : Selection
        {
            var _loc_2:int = 0;
            var _loc_1:* = new Selection(this.mask.width, this.mask.height);
            _loc_1.mask.copyPixels(this.mask, this.mask.rect, this.mask.rect.topLeft);
            _loc_1.selected = this.selected.clone();
            _loc_1.outline = new Vector.<OutlinePoint>;
            while (_loc_2 < this.outline.length)
            {
                
                _loc_1.outline.push(this.outline[_loc_2]);
                _loc_2++;
            }
            return _loc_1;
        }// end function

    }
}
